import { director } from 'cc';
class SceneStackData {
  public static HomePage = 'HomePage';
  public static RoomPage = 'RoomPage';
  public static SelectPage = 'SelectPage';
  public static MatchPage = 'MatchPage';
  public static RankingPage = 'RankingPage';
  public static UserCenterPage = 'UserCenterPage';
  public static EarnCoinPage = 'EarnCoinPage';
  public static BackPackPage = 'BackPackPage';
  public static MoreGamePage = 'MoreGamePage';
  public scene: string = '';
  public data: any = null;
  constructor(s: string) {
    this.scene = s;
  }
}

export class SceneStack {
  private stacks: Array<SceneStackData> = [];
  // 进入到一个场景页面
  public pushScene(ssd: SceneStackData) {
    this.stacks.push(ssd);
    director.loadScene(ssd.scene);
  }
  // 返回上一层页面
  public backScene() {
    this.stacks.pop();
    if (this.stacks.length >= 1) {
      const ssd = this.stacks[this.stacks.length - 1];
      director.loadScene(ssd.scene);
    } else {
      this.toHomePage();
    }
  }

  // 回到首页可以清空信息
  public backHomePage() {
    this.stacks.length = 0;
    this.pushScene(new SceneStackData(SceneStackData.HomePage));
  }
  
  public toHomePage() {
    this.pushScene(new SceneStackData(SceneStackData.HomePage));
  }
  public toRoomPage() {
    this.pushScene(new SceneStackData(SceneStackData.RoomPage));
  }
  public toSelectPage() {
    this.pushScene(new SceneStackData(SceneStackData.SelectPage));
  }
  public toMatchPage() {
    this.pushScene(new SceneStackData(SceneStackData.MatchPage));
  } 
  public toUsercenterPage() {
    this.pushScene(new SceneStackData(SceneStackData.UserCenterPage));
  }
  public toEarnCoinPage() {
    this.pushScene(new SceneStackData(SceneStackData.EarnCoinPage));
  }
  public toBackPackPage() {
    this.pushScene(new SceneStackData(SceneStackData.BackPackPage));
  }
  public toRankingPage() {
    this.pushScene(new SceneStackData(SceneStackData.RankingPage));
  }
  public toMoreGamePage() {
    this.pushScene(new SceneStackData(SceneStackData.MoreGamePage));
  }
  public getStackData() {
    if (this.stacks.length >= 1) {
      const ssd = this.stacks[this.stacks.length - 1];
      return ssd.data;
    }
    return null;
  }
  public isInRoomPage() {
    if (this.stacks.length >= 1) {
      const ssd = this.stacks[this.stacks.length - 1];
      return ssd.scene === SceneStackData.RoomPage;
    }
    return false;
  }
  public currentPageName() {
    if (this.stacks.length > 0) {
      return this.stacks[this.stacks.length - 1].scene;
    }
    return SceneStackData.HomePage;
  }
  public isMatchPage() {
    if (this.stacks.length > 0) {
      return this.stacks[this.stacks.length - 1].scene === SceneStackData.MatchPage;
    }
    return false;;
  }
  public static intoRoomPageName() {
    return `Into${SceneStackData.RoomPage}`;
  }
}